<template>
  <el-container>
    <!-- 头 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏-->
      <el-aside :width='isCollapse?"50px":"200px"'>
        <div class="toggle" @click='isCollapse = !isCollapse'>|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse-transition='false'
          :collapse = 'isCollapse'
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + menuItem.path"
              v-for="menuItem in item.children"
              :key="menuItem.id"
              @click="saveActivePath('/' + menuItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ menuItem.authName }}</span></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('path');
  },
  data() {
    return {
      menuList: [],
      iconObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-view',
        101: 'el-icon-shopping-bag-2',
        102: 'el-icon-shopping-cart-2',
        145: 'el-icon-document',
      },
      isCollapse:false,
      activePath:''
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$message.success('退出成功')
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      console.log(res)
    },
    saveActivePath(activePath){
        window.sessionStorage.setItem("path",activePath);
        this.activePath = window.sessionStorage.getItem('path');
    }
  },
}
</script>

<style scpoed>
.toggle {
  width: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background-color: #545c64;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  line-height: 16px;
  font-size:14px;
}

.el-submenu{
  padding-right:-0.8px;
}

.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div span {
  color: white;
  margin-left: 15px;
}

.el-side {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}

.el-menu {
  border-right: none;
}
</style>